package com.example.hystrixdemo.controller;

import com.example.hystrixdemo.service.AccountCommand;
import com.example.hystrixdemo.service.GitHubFeignService;
import com.example.hystrixdemo.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RestController;

import java.util.concurrent.Future;

/**
 * @Package: com.example.hystrixdemo.controller
 * @ClassName: UserController
 * @Author: Think
 * @CreateTime: 2021/7/8 13:49
 * @Description:
 */
@RestController
public class UserController {

    @Autowired
    private UserService userService;
    @Autowired
    private GitHubFeignService gitHubFeignService;

    @GetMapping("/getUserAddress/{userName}")
    public String getUserAddress(@PathVariable String userName) throws Exception {
        return userService.getUserAddress(userName);
    }

    @GetMapping("/getGitHubInfo/{repoName}")
    public String getGitHubInfo(@PathVariable String repoName) throws Exception {
        return gitHubFeignService.searchRepo(repoName);
    }

    @GetMapping("/getAccount/{accountName}")
    public String getAccount(@PathVariable String accountName) throws Exception {
        // run方法的同步执行
        // return new AccountCommand(accountName).execute();
        // 异步执行run方法，适合需要同时执行多个Command的run方法场景
        Future<String> future = new AccountCommand(accountName).queue();
        return future.get();
    }

}
